package com.syxx.oa.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.syxx.oa.domain.NodeAuthority;
import com.test.jbpm.dao.impl.BaseDao;

@Repository
public class NodeAuthorityDao extends BaseDao {
	public boolean addOrUpdate(NodeAuthority bean) {
		if(bean == null){
			return false;
		}
		try {
			getHibernateTemplate().saveOrUpdate(bean);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean del(Long id){
		if(id == null || id == 0L){
			return false;
		}
		NodeAuthority nodeAuthority =(NodeAuthority) getHibernateTemplate().load(NodeAuthority.class, id);
		if(nodeAuthority.get_id() == null || nodeAuthority.get_id() == 0L){
			return false;
		}
		try {
			getHibernateTemplate().delete(nodeAuthority);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public List<NodeAuthority> findByPname(final String processName){
		@SuppressWarnings("unchecked")
		List<NodeAuthority> reList = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				String hql = "from NodeAuthority where processName = ?";
				Query query = session.createQuery(hql);
				query.setParameter(0, processName);
				return query.list();
			}
		});
		return reList;
	}
}
